﻿
Partial Class WebForm_ManageWorkContent_SearchCleanPlan
    Inherits System.Web.UI.UserControl
    Dim DT, CleanItemDT As Data.DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLOrganization()
            Me.BindDDLPlanItemType()
            RaiseEvent Loaded(Me, New EventArgs)
        End If
    End Sub

#Region "Gridview事件"

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.Header Then
            Me.BindCleanPlanItem()
            Me.BindCleanPlanMapping()
        End If
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim haveAccess As New MyBoolean

            haveAccess.Add(Not TypeOf DataBinder.Eval(e.Row.DataItem, "HaveAccess") Is DBNull AndAlso DataBinder.Eval(e.Row.DataItem, "HaveAccess") = "Y")

            '刪除和編輯按鈕權限
            If haveAccess.Value = True Then
                TryCast(e.Row.FindControl("lknDelete"), LinkButton).Visible = True
                TryCast(e.Row.FindControl("mvForm"), MultiView).SetActiveView(TryCast(e.Row.FindControl("vwEdit"), View))
            Else
                TryCast(e.Row.FindControl("lknDelete"), LinkButton).Visible = False
                TryCast(e.Row.FindControl("mvForm"), MultiView).SetActiveView(TryCast(e.Row.FindControl("vwSelect"), View))
            End If

            Dim TempDT As Data.DataTable
            Dim TempDataView As Data.DataView = DT.DefaultView
            Dim key As DataKey = Me.gv.DataKeys(e.Row.RowIndex)
            TempDataView.RowFilter = "PlanID='" + key("PlanID").ToString() + "'"
            TempDT = TempDataView.ToTable

            If Not TempDT.Rows.Count <= 0 Then
                TryCast(e.Row.FindControl("lknDelete"), LinkButton).Visible = False
            End If

            ''取出清潔內容
            'TempDataView = CleanItemDT.DefaultView
            'TempDataView.RowFilter = "PlanID='" + key("PlanID").ToString() + "'"
            'TempDT = TempDataView.ToTable
            ''For i As Integer = 0 To TempDT.Rows.Count - 1
            ''    If i = 0 Then
            ''        TryCast(e.Row.FindControl("lblWorkItem"), Label).Text = TempDT.Rows(i)("Text").ToString()
            ''    Else
            ''        TryCast(e.Row.FindControl("lblWorkItem"), Label).Text &= "、" + TempDT.Rows(i)("Text").ToString()
            ''    End If
            ''Next


        End If
    End Sub

    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        Dim key As DataKey = Me.gv.DataKeys(e.RowIndex)
        Me.SelectedPlanID = key("PlanID")
        RaiseEvent Deleting(Me, New EventArgs)
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub
#End Region

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub

    Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing
        Dim key As DataKey = Me.gv.DataKeys(e.NewEditIndex)
        Me.SelectedPlanID = key("PlanID")
        RaiseEvent Updating(Me, New EventArgs)

        e.Cancel = True
    End Sub

    Protected Sub GV_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.SelectedIndexChanged
        Me.SelectedPlanID = Me.gv.SelectedDataKey("PlanID")
        RaiseEvent SelectedIndexChanged(Me, New EventArgs)
    End Sub

    Protected Sub btnAddPlanItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddPlanItem.Click
        RaiseEvent Add(Me, New EventArgs)
    End Sub
End Class

Partial Class WebForm_ManageWorkContent_SearchCleanPlan
#Region "繫結資料"
    Private Sub BindDDLOrganization()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select o.* ")
        sql.AppendFormat("from OrganizationView o ")
        sql.AppendFormat("where OrganizationName like '%公所%' and Active = 'Y' ")

        Me.ddlOrganization.DataTextField = "OrganizationName"
        Me.ddlOrganization.DataValueField = "UOrganizationID"
        'Me.ddlOrganization.DataSource = MyObj.GetDDLOrganizationData()
        'Me.ddlOrganization.DataSource = dat.GetData(sql)
        Me.ddlOrganization.DataSource = MyObj.GetDDLDirectOrganizationData()
        'Me.ddlOrganization.DataSource = MyObj.GetDDLDirectOrganizationData()
        Me.ddlOrganization.DataBind()

        If Me.ddlOrganization.Items.Count > 1 Then
            Me.ddlOrganization.Items.Insert(0, MyObj.GetDDLSelectAllItem())
        End If

    End Sub

    Private Sub BindDDLPlanItemType()

        Me.ddlPlanItemType.Items.Clear()

        Me.ddlPlanItemType.DataTextField = "Text"
        Me.ddlPlanItemType.DataValueField = "Value"

        Me.ddlPlanItemType.DataSource = MyObj.GetCodeMappingData("CleanPlan", "ItemType")
        Me.ddlPlanItemType.DataBind()

        Me.ddlPlanItemType.Items.Insert(0, MyObj.GetDDLSelectAllItem())

    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyOrganizationDataFilter(True)
        Dim dt As Data.DataTable

        sql.AppendFormat("select cp.*,cm.Text as ItemTypeName,u.UserName, o.OrganizationName, '環境清潔及雜物整理' as WorkItemName ")
        sql.AppendFormat(" from CleanPlan cp")
        sql.AppendFormat(" inner join OrganizationView o on cp.UOrganizationID = o.UOrganizationID ")
        sql.AppendFormat(" left join [User] u on cp.PersonInCharge = u.UserID ")
        sql.AppendFormat(" left join CodeMapping cm on cp.ItemType = cm.Value And cm.TableName='CleanPlan' And cm.ColumnName='ItemType' ")

        If Me.ddlOrganization.SelectedValue <> "" Then
            sql.AppendFormat(" inner join OrganizationTree ot on cp.UOrganizationID = ot.UOrganizationID ")
        End If

        sql.AppendFormat(" where 1=1 ")
        sql.AppendFormat(Me.GetWhereSQL())
        sql.AppendFormat(" order by CreateDate Desc")
        dt = dat.GetData(sql)

        Me.gv.DataSource = dt
        Me.gv.DataBind()
    End Sub

    Private Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder

        'sql.AppendFormat(" and a.Status = '{0}' ", Me.ddlStatus.SelectedValue)

        If Me.ddlOrganization.SelectedValue <> "" Then
            sql.AppendFormat(" and ot.URoot = '{0}' ", Me.ddlOrganization.SelectedValue)
        End If

        If Me.AnnualControl1.Anaual <> "" Then
            sql.AppendFormat(" and year(cp.StartDate) = {0} ", Me.AnnualControl1.Anaual)
        End If

        If Me.ddlPlanItemType.SelectedValue <> "" Then
            sql.AppendFormat(" and cp.ItemType = {0} ", Me.ddlPlanItemType.SelectedValue)
        End If
        
        Return sql.ToString
    End Function

    Public Sub BindCleanPlanItem()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("Select a.*,b.Text from CleanPlanItem a")
        sql.AppendFormat(" Join CodeMapping b")
        sql.AppendFormat(" on a.CleanItemID=b.Value")
        sql.AppendFormat(" and b.TableName='CleanPlan' and b.ColumnName='CleanItem'")
        CleanItemDT = dat.GetData(sql)
    End Sub

    Public Sub BindCleanPlanMapping()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("Select * from CleanPlanMapping")
        DT = dat.GetData(sql)
    End Sub
#End Region

#Region "屬性"
    Public Property SelectedPlanID() As String
        Get
            Return ViewState("SelectedPlanID")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedPlanID") = value
        End Set
    End Property
#End Region

#Region "事件"
    Public Event Loaded(ByVal source As Object, ByVal e As EventArgs)
    Public Event DataBound(ByVal source As Object, ByVal e As EventArgs)
    Public Event Updating(ByVal source As Object, ByVal e As EventArgs)
    Public Event Deleting(ByVal source As Object, ByVal e As EventArgs)
    Public Event Add(ByVal source As Object, ByVal e As EventArgs)
    Public Event SelectedIndexChanged(ByVal source As Object, ByVal e As EventArgs)
#End Region
End Class